//
//  HSQScreeningViewController.m
//  LiTianDecoration
//
//  Created by administrator on 2018/5/3.
//  Copyright © 2018年 administrator. All rights reserved.
//

#import "HSQScreeningViewController.h"
#import "HSQScreeningHeaderReusableView.h"
#import "HSQScreeningListCollectionViewCell.h"
#import "HSQSinceBuyCountCollectionViewCell.h"
#import "HSQBtnListCollectionViewCell.h"
#import "HSQFilterCategoryListModel.h"

@interface HSQScreeningViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,HSQBtnListCollectionViewCellDelegate>

@property (nonatomic, strong) UIButton *confirm_button; // 确认按钮

@property (nonatomic, strong) UIButton *reset_button; // 重置按钮

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation HSQScreeningViewController

- (NSMutableArray *)dataSource{
    
    if (_dataSource == nil) {
        
        self.dataSource = [NSMutableArray array];
    }
    
    return _dataSource;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title  = @"商品筛选";
    
    // 设置底部的按钮
    [self SetTheBottomButton];
    
    // 创建集合视图
    [self SetUpCollectionView];
    
    // 添加筛选的数据
    [self AddFilteredData];
}

/**
 * @brief 设置底部的按钮
 */
- (void)SetTheBottomButton{
    
    // 重置按钮
    UIButton *reset_button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [reset_button setTitle:@"重置" forState:(UIControlStateNormal)];
    reset_button.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [reset_button setTitleColor:RGB(71, 71, 71) forState:(UIControlStateNormal)];
    [reset_button setBackgroundImage:[UIImage ImageWithColor:[UIColor whiteColor]] forState:(UIControlStateNormal)];
    [reset_button addTarget:self action:@selector(reset_buttonClickAction:) forControlEvents:(UIControlEventTouchUpInside)];
    reset_button.frame = CGRectMake(0, KScreenHeight - KSafeTopeHeight - KSafeBottomHeight - 49, KScreenWidth / 2, 49);
    [self.view addSubview:reset_button];
    self.reset_button = reset_button;
    
    // 确认按钮
    UIButton *confirm_button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [confirm_button setTitle:@"确认" forState:(UIControlStateNormal)];
    confirm_button.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [confirm_button setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [confirm_button setBackgroundImage:[UIImage ImageWithColor:[UIColor redColor]] forState:(UIControlStateNormal)];
    [confirm_button addTarget:self action:@selector(confirm_buttonClickAction:) forControlEvents:(UIControlEventTouchUpInside)];
    confirm_button.frame = CGRectMake(KScreenWidth / 2, KScreenHeight - KSafeTopeHeight - KSafeBottomHeight - 49, KScreenWidth / 2, 49);
    [self.view addSubview:confirm_button];
    self.confirm_button = confirm_button;
}

/**
 * @brief 重置按钮的点击confirm_buttonClickAction
 */
- (void)reset_buttonClickAction:(UIButton *)sender{
    
    
}

/**
 * @brief 确认按钮的点击
 */
- (void)confirm_buttonClickAction:(UIButton *)sender{
    
    
}

/**
 * @brief 创建集合视图
 */
- (void)SetUpCollectionView{
    
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
    [flowlayout setScrollDirection:UICollectionViewScrollDirectionVertical]; //设置滚动方向
    flowlayout.minimumInteritemSpacing = 0; //左右间距
    flowlayout.minimumLineSpacing = 5; //上下间距
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0 , 0, KScreenWidth, KScreenHeight - KSafeTopeHeight - 49 - KSafeBottomHeight) collectionViewLayout:flowlayout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.showsHorizontalScrollIndicator = NO;
    [collectionView setBackgroundColor:[UIColor clearColor]];
    
    // 分区头部的视图
    [collectionView registerClass:[HSQScreeningHeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HSQScreeningHeaderReusableView"];
    
    // 价格区间
    [collectionView registerClass:[HSQScreeningListCollectionViewCell class] forCellWithReuseIdentifier:@"HSQScreeningListCollectionViewCell"];
    
    // 起购量
    [collectionView registerClass:[HSQSinceBuyCountCollectionViewCell class] forCellWithReuseIdentifier:@"HSQSinceBuyCountCollectionViewCell"];
    
    // 筛选按钮的listCell
    [collectionView registerClass:[HSQBtnListCollectionViewCell class] forCellWithReuseIdentifier:@"HSQBtnListCollectionViewCell"];
    
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
    
}

/**
 * @brief 添加筛选的数据
 */
- (void)AddFilteredData{
    
    HSQFilterCategoryListModel *CategoryModel = [[HSQFilterCategoryListModel alloc] init];
    CategoryModel.categoryName = @"价格区间";
    CategoryModel.categoryList = [NSMutableArray arrayWithObjects:@"", nil];
    [self.dataSource addObject:CategoryModel];
    
    HSQFilterCategoryListModel *CategoryModel02 = [[HSQFilterCategoryListModel alloc] init];
    CategoryModel02.categoryName = @"起购量";
    CategoryModel02.categoryList = [NSMutableArray arrayWithObjects:@"", nil];
    [self.dataSource addObject:CategoryModel02];
    
    HSQFilterCategoryListModel *CategoryModel03 = [[HSQFilterCategoryListModel alloc] init];
    CategoryModel03.categoryName = @"包邮";
    CategoryModel03.categoryList = [NSMutableArray array];
    HSQFilterAttributeListModel *AttributeModel = [[HSQFilterAttributeListModel alloc] init];
    AttributeModel.attributeName = @"包邮";
    [CategoryModel03.categoryList addObject:AttributeModel];
    [self.dataSource addObject:CategoryModel03];
    
    HSQFilterCategoryListModel *CategoryModel04 = [[HSQFilterCategoryListModel alloc] init];
    CategoryModel04.categoryName = @"店铺券";
    CategoryModel04.categoryList = [NSMutableArray array];
    HSQFilterAttributeListModel *AttributeModel01 = [[HSQFilterAttributeListModel alloc] init];
    AttributeModel01.attributeName = @"店铺券";
    [CategoryModel04.categoryList addObject:AttributeModel01];
    [self.dataSource addObject:CategoryModel04];
    
    HSQFilterCategoryListModel *CategoryModel05 = [[HSQFilterCategoryListModel alloc] init];
    CategoryModel05.categoryName = @"促销活动";
    CategoryModel05.categoryList = [NSMutableArray array];
    HSQFilterAttributeListModel *AttributeModel02 = [[HSQFilterAttributeListModel alloc] init];
    AttributeModel02.attributeName = @"限时折扣";
    [CategoryModel05.categoryList addObject:AttributeModel02];
    
    HSQFilterAttributeListModel *AttributeModel021 = [[HSQFilterAttributeListModel alloc] init];
    AttributeModel021.attributeName = @"全款预售";
    [CategoryModel05.categoryList addObject:AttributeModel021];
    
    HSQFilterAttributeListModel *AttributeModel022 = [[HSQFilterAttributeListModel alloc] init];
    AttributeModel022.attributeName = @"定金预售";
    [CategoryModel05.categoryList addObject:AttributeModel022];
    
    HSQFilterAttributeListModel *AttributeModel023 = [[HSQFilterAttributeListModel alloc] init];
    AttributeModel023.attributeName = @"秒杀活动";
    [CategoryModel05.categoryList addObject:AttributeModel023];
    
    [self.dataSource addObject:CategoryModel05];
    
    HSQFilterCategoryListModel *CategoryModel06 = [[HSQFilterCategoryListModel alloc] init];
    CategoryModel06.categoryName = @"经营类型";
    CategoryModel06.categoryList = [NSMutableArray array];
    
    HSQFilterAttributeListModel *AttributeModel03 = [[HSQFilterAttributeListModel alloc] init];
    AttributeModel03.attributeName = @"平台自营";
    [CategoryModel06.categoryList addObject:AttributeModel03];
    
    HSQFilterAttributeListModel *AttributeModel031 = [[HSQFilterAttributeListModel alloc] init];
    AttributeModel031.attributeName = @"三方店铺";
    AttributeModel031.IsSelect_State = @"1";
    [CategoryModel06.categoryList addObject:AttributeModel031];
    
    [self.dataSource addObject:CategoryModel06];
    
    HSQFilterCategoryListModel *CategoryModel07 = [[HSQFilterCategoryListModel alloc] init];
    CategoryModel07.categoryName = @"销售模式";
    CategoryModel07.categoryList = [NSMutableArray array];
    
    HSQFilterAttributeListModel *AttributeModel041 = [[HSQFilterAttributeListModel alloc] init];
    AttributeModel041.attributeName = @"零售型";
    [CategoryModel07.categoryList addObject:AttributeModel041];
    
    HSQFilterAttributeListModel *AttributeModel042 = [[HSQFilterAttributeListModel alloc] init];
    AttributeModel042.attributeName = @"批发型";
    [CategoryModel07.categoryList addObject:AttributeModel042];
    
    HSQFilterAttributeListModel *AttributeModel043 = [[HSQFilterAttributeListModel alloc] init];
    AttributeModel043.attributeName = @"虚拟型";
    [CategoryModel07.categoryList addObject:AttributeModel043];
    
    HSQFilterAttributeListModel *AttributeModel044 = [[HSQFilterAttributeListModel alloc] init];
    AttributeModel044.attributeName = @"海外型";
    [CategoryModel07.categoryList addObject:AttributeModel044];
    
    [self.dataSource addObject:CategoryModel07];
    
    HSQFilterCategoryListModel *CategoryModel08 = [[HSQFilterCategoryListModel alloc] init];
    CategoryModel08.categoryName = @"赠品";
    CategoryModel08.categoryList = [NSMutableArray array];
    
    HSQFilterAttributeListModel *AttributeModel051 = [[HSQFilterAttributeListModel alloc] init];
    AttributeModel051.attributeName = @"赠品";
    [CategoryModel08.categoryList addObject:AttributeModel051];
    
    [self.dataSource addObject:CategoryModel08];
    
}






- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return self.dataSource.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    HSQFilterCategoryListModel *model = self.dataSource[section];
    
    return model.categoryList.count;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(KScreenWidth, 44);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0 || indexPath.section == 1)
    {
        return CGSizeMake(KScreenWidth, 44);
    }
    else
    {
        return CGSizeMake((KScreenWidth) / 3, 44);
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    HSQScreeningHeaderReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HSQScreeningHeaderReusableView" forIndexPath:indexPath];
    
    HSQFilterCategoryListModel *categoryModel = self.dataSource[indexPath.section];
    
    headView.title_label.text = categoryModel.categoryName;
    
    return headView;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0)
    {
        HSQScreeningListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HSQScreeningListCollectionViewCell" forIndexPath:indexPath];
        
        return cell;
    }
    else  if (indexPath.section == 1)
    {
        HSQSinceBuyCountCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HSQSinceBuyCountCollectionViewCell" forIndexPath:indexPath];
        
        return cell;
    }
    else
    {
        HSQBtnListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HSQBtnListCollectionViewCell" forIndexPath:indexPath];
        
        HSQFilterCategoryListModel *categoryModel = self.dataSource[indexPath.section];
        
        cell.model = categoryModel.categoryList[indexPath.row];
        
        cell.delegate = self;
        
        return cell;
    }
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    

}

/**
 * @brief 筛选属性的按钮点击事件
 */
- (void)FilterTheClickOfThePropertiesButton:(UIButton *)sender{
    
    HSQBtnListCollectionViewCell *cell = (HSQBtnListCollectionViewCell *)sender.superview.superview;
    
    NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
    
    HSQFilterCategoryListModel *categoryModel = self.dataSource[indexPath.section];
    
    HSQFilterAttributeListModel *AttributeModel = categoryModel.categoryList[indexPath.row];
    
    if (AttributeModel.IsSelect_State.integerValue == 0)
    {
        AttributeModel.IsSelect_State = @"1";
    }
    else
    {
        AttributeModel.IsSelect_State = @"0";
    }
    
    [self.collectionView reloadData];
}


















@end
